import Cookies from "js-cookie";

const TokenKey = "authorized-token";

type paramsMapType = {
  accessToken: string;
  refreshToken: string;
};

// 获取token
export function getToken() {
  // 此处与TokenKey相同，此写法解决初始化时Cookies中不存在TokenKey报错
  return Cookies.get("authorized-token");
}

// 设置token
export function setToken(data:any) {
  const { accessToken, expireTime, refreshToken } = data;
  // 提取关键信息进行存储
  const paramsMap: paramsMapType = {
    accessToken,
    refreshToken,
  };
  const dataString = JSON.stringify(paramsMap);
  Cookies.set(TokenKey, dataString);
}

// 删除token
export function removeToken() {
  Cookies.remove(TokenKey);
}